package com.icss.ah.postTrade.service; 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.SyncSelectTree;

@Service
@Transactional
public class PostTradeService {
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	/**
	 * 商品类目列表查询
	 * @return
	 */
	public List<Map<String, Object>> getCategoryList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getCategoryList",map);
			infolog.info("PostTradeService|getCategoryList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getCategoryList",e);
		}
		return list;
	}
	
	
	/**
	 * 类目搜索
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> queryCategoryList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.queryCategoryList",map);
			infolog.info("PostTradeService|queryCategoryList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|queryCategoryList",e);
		}
		return list;
	}
	
	/**
	 * 商品参数
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getBaseParam(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getBaseParam",map);
			infolog.info("PostTradeService|getBaseParam");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getBaseParam",e);
		}
		return list;
	}
	
	/**
	 * 字典查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getDirectoryInfo(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getDirectoryInfo",map);
			infolog.info("PostTradeService|getDirectoryInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getDirectoryInfo",e);
		}
		return list;
	}
	
	/**
	 * 商品销售区域查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> queryShopAreaList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.queryProductAreaList",map);
			if(list.isEmpty())
				list = sqlSessionTemplate.selectList("PostTradeBean.queryShopAreaList",map);
			infolog.info("PostTradeService|queryShopAreaList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|queryShopAreaList",e);
		}
		return list;
	}
	
	public List<SyncSelectTree> getTree(Map<String, String> map) {
//		SyncSelectTree selectTree
//		String id = selectTree.getId();//id 异步加载展开树时传到后台的id
//		String ifAll = selectTree.getIfAll();//是否一次性展示所有数据，1是 0否
		List<SyncSelectTree> list = new ArrayList<SyncSelectTree>();
		try {
//			if (ifAll != null && !"".equals(ifAll)) {
//				if ("0".equals(ifAll)) {//展开树时加载数据
//					if ("".equals(id)) {//如果查询id没有值，表示查询根节点
//						list = sqlSessionTemplate.selectList("PostTradeBean.getTree",selectTree);
//					}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
//						list = sqlSessionTemplate.selectList("PostTradeBean.getTreeChild",selectTree);
//					}
//				}else{//一次加载所有数据
//					if ("".equals(id)) {//如果查询id没有值，表示查询根节点
//						list = sqlSessionTemplate.selectList("PostTradeBean.getTreeAll",selectTree);
//					}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
//						list = sqlSessionTemplate.selectList("PostTradeBean.getTreeChild",selectTree);
//					}
//				}
//			}
			
			/////取店铺销售区域
			List<Map<String, Object>> areaList = sqlSessionTemplate.selectList("PostTradeBean.getShopArea",map);
			Set<String> set = new HashSet<String>();
			if(areaList != null && !areaList.isEmpty()){
				for(int i=0;i<areaList.size();i++){
					Map<String, Object> obj = areaList.get(i);
					String areaCode = (String) obj.get("AREA_CODE");
					set.add(areaCode);
					set.add(areaCode.substring(0, 9));
					set.add(areaCode.substring(0, 6));
					set.add(areaCode.substring(0, 3));
				}
			}
			String areaStr = "'0'";
			for(String code : set){
				areaStr += ",'"+code+"'";
			}
			//areaStr = areaStr.substring(2);
			map.put("areaStr", areaStr);
			////////
			
			list = sqlSessionTemplate.selectList("PostTradeBean.getTreeAll",map);
			infolog.info("PostTradeService|getTree|list:"+list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getTree", e);
		}
		return list == null ? new ArrayList<SyncSelectTree>() : list;
	}
	
	/**
	 * 计量单位查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getMeasuringInfo(){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getMeasuringInfo");
			infolog.info("PostTradeService|getMeasuringInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getMeasuringInfo",e);
		}
		return list;
	}
	
	/**
	 * 店铺类别
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getShopType(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getShopType",map);
			infolog.info("PostTradeService|getShopType");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getShopType",e);
		}
		return list;
	}
	/**
	 * 保存商品信息
	 * @param map
	 * @return
	 */
	@Transactional
	public boolean saveProductInfo(Map<String, String> map,List<Map<String, String>> listPara,List<Map<String, String>> listKey,
			List<Map<String, String>> listBuy,List<Map<String, String>> listImg,List<Map<String, String>> areaList){
		boolean result = false;
		try {
			Integer count = sqlSessionTemplate.selectOne("PostTradeBean.getProductCount", map);
			if(count>0){
				throw new Exception("该商品已存在");
			}
			//新增商品信息
			sqlSessionTemplate.insert("PostTradeBean.insertProduct",map);
			//新增商品购进信息
			map.put("uuid", UUID.randomUUID().toString());
			sqlSessionTemplate.insert("PostTradeBean.insertProductBuy",map);
			//新增商品审核信息
			sqlSessionTemplate.insert("PostTradeBean.insertProductAudit",map);
			if(!listPara.isEmpty()){
				//保存商品参数
				sqlSessionTemplate.insert("PostTradeBean.insertProPara",listPara);
			}
			if(!listKey.isEmpty()){
				//保存商品关键字
				sqlSessionTemplate.insert("PostTradeBean.insertProKey",listKey);
			}
			if(!listBuy.isEmpty()){
				//保存商品价格
				sqlSessionTemplate.insert("PostTradeBean.insertProPrice",listBuy);
			}
			if(!listImg.isEmpty()){
				//保存商品图片
				sqlSessionTemplate.insert("PostTradeBean.insertProPic",listImg);
			}
			//新增商品投放区域
			if(areaList.isEmpty())
				sqlSessionTemplate.insert("PostTradeBean.insertProductArea",map);
			else
				sqlSessionTemplate.insert("PostTradeBean.insertProductAreaList",areaList);
				
			result = true;
			infolog.info("PostTradeService|insertProduct success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|insertProduct",e);
		}
		return result;
	}
	/**
	 * 保存商品信息
	 * @param map
	 * @return
	 */
	public void insertProduct(Map<String, String> map){
		try {
			sqlSessionTemplate.insert("PostTradeBean.insertProduct",map);
			map.put("uuid", UUID.randomUUID().toString());
			sqlSessionTemplate.insert("PostTradeBean.insertProductBuy",map);
			infolog.info("PostTradeService|insertProduct success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|insertProduct",e);
		}
	}
	
	/**
	 * 保存商品参数
	 * @param map
	 * @return
	 */
	public void insertProPara(List<Map<String, String>> list){
		try {
			sqlSessionTemplate.insert("PostTradeBean.insertProPara",list);
			infolog.info("PostTradeService|insertProPara success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|insertProPara",e);
		}
	}
	
	/**
	 * 保存商品关键词
	 * @param map
	 * @return
	 */
	public void insertProKey(List<Map<String, String>> list){
		try {
			sqlSessionTemplate.insert("PostTradeBean.insertProKey",list);
			infolog.info("PostTradeService|insertProKey success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|insertProKey",e);
		}
	}
	
	/**
	 * 保存商品价格
	 * @param map
	 * @return
	 */
	public void insertProPrice(List<Map<String, String>> list){
		try {
			sqlSessionTemplate.insert("PostTradeBean.insertProPrice",list);
			infolog.info("PostTradeService|insertProPrice success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|insertProPrice",e);
		}
	}
	
	/**
	 * 保存商品图片
	 * @param map
	 * @return
	 */
	public void insertProPic(List<Map<String, String>> list){
		try {
			sqlSessionTemplate.insert("PostTradeBean.insertProPic",list);
			infolog.info("PostTradeService|insertProPic success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|insertProPic",e);
		}
	}
	
	/**
	 * 复制
	 * @param map
	 * @return
	 */
	public boolean copyProduct(HashMap<String, String> map){
		boolean result = false;
		try {
			sqlSessionTemplate.update("PostTradeBean.copyProduct",map);
			result = true;
			infolog.info("PostTradeService|insertProPic success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|insertProPic",e);
		}
		return result;
	}
	
	/**
	 * 查询审核信息
	 * @param uuid
	 * @return
	 */
	public Map<String, Object> getAuditInfo(String uuid){
		Map<String, Object> info = new HashMap<String, Object>();
		try {
			info = sqlSessionTemplate.selectOne("PostTradeBean.getAuditInfo",uuid);
			infolog.info("PostTradeService|getAuditInfo success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getAuditInfo",e);
		}
		return info;
	}
	
	
	/**
	 * 查询商品信息
	 * @param map
	 * @return
	 */
	public Map<String, Object> getProInfo(Map<String, String> map){
		Map<String, Object> info = new HashMap<String, Object>();
		try {
			info = sqlSessionTemplate.selectOne("PostTradeBean.getProInfo",map);
			infolog.info("PostTradeService|getProInfo success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getProInfo",e);
		}
		return info;
	}
	
	/**
	 * 查询商品参数
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getProParam(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getProParam",map);
			infolog.info("PostTradeService|getProParam success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getProParam",e);
		}
		return list;
	}
	
	/**
	 * 查询商品图片
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getProPic(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getProPic",map);
			infolog.info("PostTradeService|getProPic success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getProPic",e);
		}
		return list;
	}
	
	/**
	 * 查询商品关键字
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getKeyWord(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getKeyWord",map);
			infolog.info("PostTradeService|getKeyWord success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getKeyWord",e);
		}
		return list;
	}
	
	/**
	 * 查询商品价格
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getProPrice(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.getProPrice",map);
			infolog.info("PostTradeService|getProPrice success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|getProPrice",e);
		}
		return list;
	}
	
	/**
	 * 更新商品信息
	 * @param map
	 */
	public void updatePro(Map<String, String> map){
		try {
			sqlSessionTemplate.update("PostTradeBean.updatePro", map);
			infolog.info("PostTradeService|updatePro success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|updatePro",e);
		}
	}
	
	/**
	 * 商品参数更新
	 * @param list
	 */
	public void updatePara(List<Map<String, String>> list){
		try {
			List<Map<String, String>> paraList = new ArrayList<Map<String,String>>();
			for(int i=0;i<list.size();i++){
				Map<String, String> m = list.get(i);
				String uuid = m.get("paraUuid");
				if("-1".equals(uuid)){
					uuid = UUID.randomUUID().toString();
					m.put("paraUuid", uuid);
					paraList.add(m);
				}else{
					sqlSessionTemplate.update("PostTradeBean.updatePara", m);
					infolog.info("PostTradeService|updatePara success");
				}
			}
			if(paraList.size()>0){//新增参数
				sqlSessionTemplate.insert("PostTradeBean.insertProPara",paraList);
				infolog.info("PostTradeService|updatePara/insert success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|updatePara",e);
		}
	}
	
	/**
	 * 商品价格更新
	 * @param list
	 */
	public void updateProPrice(List<Map<String, String>> list){
		try {
			List<Map<String, String>> pirceList = new ArrayList<Map<String,String>>();
			for(int i=0;i<list.size();i++){
				Map<String, String> m = list.get(i);
				String uuid = m.get("pUuid");
				if("-1".equals(uuid)){
					uuid = UUID.randomUUID().toString();
					m.put("pUuid", uuid);
					pirceList.add(m);
				}else{
					sqlSessionTemplate.update("PostTradeBean.updateProPrice", m);
					infolog.info("PostTradeService|updateProPrice success");
				}
			}
			if(pirceList.size()>0){//新增价格
				sqlSessionTemplate.insert("PostTradeBean.insertProPrice",pirceList);
				infolog.info("PostTradeService|updateProPrice/insert success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|updateProPrice",e);
		}
	}
	
	/**
	 * 商品图片更新
	 * @param list
	 */
	public void updatePic(List<Map<String, String>> list){
		try {
			List<Map<String, String>> picList = new ArrayList<Map<String,String>>();
			for(int i=0;i<list.size();i++){
				Map<String, String> m = list.get(i);
				String uuid = m.get("picUuid");
				if("-1".equals(uuid)){
					uuid = UUID.randomUUID().toString();
					m.put("picUuid", uuid);
					picList.add(m);
				}else{
					sqlSessionTemplate.update("PostTradeBean.updatePic", m);
					infolog.info("PostTradeService|updatePic success");
				}
			}
			if(picList.size()>0){//新增图片
				sqlSessionTemplate.insert("PostTradeBean.insertProPic",picList);
				infolog.info("PostTradeService|updatePic/insert success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|updatePic",e);
		}
	}
	
	/**
	 * 商品关键词更新
	 * @param list
	 */
	public void updateProKey(List<Map<String, String>> list){
		try {
			List<Map<String, String>> picList = new ArrayList<Map<String,String>>();
			for(int i=0;i<list.size();i++){
				Map<String, String> m = list.get(i);
				String uuid = m.get("keyUuid");
				if("-1".equals(uuid)){
					uuid = UUID.randomUUID().toString();
					m.put("keyUuid", uuid);
					picList.add(m);
				}else{
					sqlSessionTemplate.update("PostTradeBean.updateProKey", m);
					infolog.info("PostTradeService|updateProKey success");
				}
			}
			if(picList.size()>0){//新增关键字
				sqlSessionTemplate.insert("PostTradeBean.insertProKey",picList);
				infolog.info("PostTradeService|updateProKey/insert success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|updateProKey",e);
		}
	}
	
	/**
	 * 商品信息更新
	 * @param 
	 */
	public String updateInfo(Map<String, String> map,List<Map<String, String>> listPara,List<Map<String, String>> listKey,
			List<Map<String, String>> listBuy,List<Map<String, String>> listImg,List<Map<String, String>> areaList){
		String result = "0";
		try {
			
			sqlSessionTemplate.update("PostTradeBean.updatePro", map);
			
			sqlSessionTemplate.delete("PostTradeBean.delProParam", map);
			sqlSessionTemplate.delete("PostTradeBean.delProPrice", map);
			sqlSessionTemplate.delete("PostTradeBean.delProPic", map);
			sqlSessionTemplate.delete("PostTradeBean.delProKey", map);
			sqlSessionTemplate.delete("PostTradeBean.delProductArea", map);
			
			if(!listPara.isEmpty()) sqlSessionTemplate.insert("PostTradeBean.insertProPara",listPara);
			
			if(!listKey.isEmpty()) sqlSessionTemplate.insert("PostTradeBean.insertProKey",listKey);
			
			if(!listBuy.isEmpty()) sqlSessionTemplate.insert("PostTradeBean.insertProPrice",listBuy);
			
			if(!listImg.isEmpty()) sqlSessionTemplate.insert("PostTradeBean.insertProPic",listImg);
			
			//新增商品投放区域
			if(areaList.isEmpty())
				sqlSessionTemplate.insert("PostTradeBean.insertProductArea",map);
			else
				sqlSessionTemplate.insert("PostTradeBean.insertProductAreaList",areaList);
				
			infolog.info("PostTradeService|updateInfo success");
			result = "1";
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|updateInfo",e);
		}
		return result;
	}
	
	public void delPro(Map<String, String> map){
		sqlSessionTemplate.delete("PostTradeBean.delProParam", map);
		sqlSessionTemplate.delete("PostTradeBean.delProPrice", map);
		sqlSessionTemplate.delete("PostTradeBean.delProPic", map);
		sqlSessionTemplate.delete("PostTradeBean.delProKey", map);
	}
	
	public String getCascadeName(String val){
		Map<String, Object> obj = proCategory(val);
		if(obj == null) return "";
		String pCode = (String) obj.get("CATEGORY_PCODE");
		if(null == pCode || "".equals(pCode)){
			return (String) obj.get("CATEGORY_NAME");
		}else{
			return getCascadeName(pCode)+">>"+(String) obj.get("CATEGORY_NAME");
		}
	}
	
	public Map<String, Object> proCategory(String val){
		Map<String, Object> info = sqlSessionTemplate.selectOne("PostTradeBean.proCategory", val);
		return info;
	}
	
	public List<Map<String, String>> getCategoryNameList(Map<String, String> map){
		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		try {
			List<Map<String, Object>> infoList = sqlSessionTemplate.selectList("PostTradeBean.getCategoryNameList",map);
			for(int i=0;i<infoList.size();i++){
				Map<String, Object> info = infoList.get(i);
				String code = (String) info.get("categoryCode");
				String cascadeName = getCascadeName1(code);
				Map<String, String> m = new HashMap<String, String>();
				m.put("CATEGORY_CODE", code);
				m.put("itemName", cascadeName);
				list.add(m);
			}
			infolog.info("PostTradeService|getCategoryNameList success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|updateProKey",e);
		}
		return list;
	}
	
	public String getCascadeName1(String val){
		Map<String, Object> obj = proCategory(val);
		String pCode = (String) obj.get("CATEGORY_PCODE");
		if(null == pCode || "".equals(pCode)){
			return (String) obj.get("CATEGORY_NAME");
		}else{
			return getCascadeName1(pCode)+">"+(String) obj.get("CATEGORY_NAME");
		}
	}
	
	/**
	 * 品牌查询
	 */
	public List<Map<String, Object>> queryBrandInfoList(HashMap<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("PostTradeBean.queryBrandCodeList",map);
			infolog.info("PostTradeService|queryBrandInfoList|list"+list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeService|queryBrandInfoList",e);
		}
		return list;
	}
	
}
